﻿Pages_Game_ConfigGame = new function () {
    this.variable = new function () {
        this.GameId = -1;
        this.MinValue = 1;
        this.MaxValue = 13;
    };
    this.documentReady = function () {
        this.variable.GameId = $("#ddlGame").val();
        this.bindData();
    };
    this.bindData = function () {
        Loading.showProcess();
        $.ajax({
            type: "GET",
            url: "/handler/HandlerConfigGame.ashx",
            data: { t: "GameGetInfo", gameId: this.variable.GameId },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Pages_Game_ConfigGame.variable.MinValue = data.MinimumValue;
                Pages_Game_ConfigGame.variable.MaxValue = data.MaximumValue;
                $("#txtMinPrice").val(data.MinimumBet);
                $("#txtMaxPrice").val(data.MaximumBet);
                $("#txtRate").val(data.Rate);
                $("#txtRandomRate").val(data.RandomRate);


                $("#txtStartExtraLife").val(data.StartExtraLife);
                $("#txtExtraLife").val(data.ExtraLife);
                $("#txtMaximumExtraLife").val(data.MaximumExtraLife);

                $("#txtStartOutOf").val(data.StartOutOf);
                $("#txtOutOf").val(data.OutOf);
                $("#txtMaximumOutOf").val(data.MaximumOutOf);

                $("#txtStartNudges").val(data.StartNudges);
                $("#txtNudges").val(data.Nudges);
                $("#txtMaximumNudges").val(data.MaximumNudges);

                $("#txtStartFreeSpin").val(data.StartFreeSpin);
                $("#txtFreeSpin").val(data.FreeSpin);
                $("#txtMaximumFreeSpin").val(data.MaximumFreeSpin);
                $(".changeEvent").css("border-color", "#CCCCCC");
                Pages_Game_ConfigGame.bindRate();
                Loading.closeProcess();
            }
        });
    };
    this.bindRate = function () {
        var sHtml = "";
        var Rate = $("#txtRate").val();
        for (var i = this.variable.MinValue; i <= this.variable.MaxValue; i++) {
            var n = this.variable.MaxValue - this.variable.MinValue + 1;
            var rateHigh = (100 * (this.variable.MaxValue - i) / n);
            var rateLow = (100 * (i - this.variable.MinValue) / n);
            sHtml += "<tr>";
            sHtml += "<td>" + i + "</td>";
            sHtml += "<td>" + COMMON.roundNumber(rateHigh, 0) + "%</td>";
            sHtml += "<td>" + COMMON.roundNumber(rateLow, 0) + "%</td>";
            sHtml += "<td style=\"background: url('/images/df_tbl_headercell_bg.gif') repeat-x\">" + (rateHigh > 0 ? COMMON.roundNumber(Rate / rateHigh, 2) : "-") + "</td>";
            sHtml += "<td style=\"background: url('/images/df_tbl_headercell_bg.gif') repeat-x\">" + (rateLow > 0 ? COMMON.roundNumber(Rate / rateLow, 2) : "-") + "</td>";
            sHtml += "</tr>";
        }
        $("#tbyRate").html(sHtml);
    };
    $("#btnUpdate").live("click", function () {
        Loading.showProcess();
        var mData = {
            id: Pages_Game_ConfigGame.variable.GameId,
            minimumBet: parseInt($("#txtMinPrice").val()),
            maximumBet: parseInt($("#txtMaxPrice").val()),
            rate: parseInt($("#txtRate").val()),
            randomRate: parseInt($("#txtRandomRate").val()),
            startExtraLife: parseInt($("#txtStartExtraLife").val()),
            extraLife: parseInt($("#txtExtraLife").val()),
            maximumExtraLife: parseInt($("#txtMaximumExtraLife").val()),
            startOutOf: parseInt($("#txtStartOutOf").val()),
            outOf: parseInt($("#txtOutOf").val()),
            maximumOutOf: parseInt($("#txtMaximumOutOf").val()),
            startNudges: parseInt($("#txtStartNudges").val()),
            nudges: parseInt($("#txtNudges").val()),
            maximumNudges: parseInt($("#txtMaximumNudges").val()),
            startFreeSpin: parseInt($("#txtStartFreeSpin").val()),
            freeSpin: parseInt($("#txtFreeSpin").val()),
            maximumFreeSpin: parseInt($("#txtMaximumFreeSpin").val())
        };

        if (!(mData.minimumBet > 0)) mData.minimumBet = 0;
        if (!(mData.maximumBet > 0)) mData.maximumBet = 0;
        if (!(mData.rate > 0)) mData.rate = 0;
        if (!(mData.randomRate > 0)) mData.randomRate = 0;
        if (!(mData.startExtraLife > 0)) mData.startExtraLife = 0;
        if (!(mData.extraLife > 0)) mData.extraLife = 0;
        if (!(mData.maximumExtraLife > 0)) mData.maximumExtraLife = 0;
        if (!(mData.startOutOf > 0)) mData.startOutOf = 0;
        if (!(mData.outOf > 0)) mData.outOf = 0;
        if (!(mData.maximumOutOf > 0)) mData.maximumOutOf = 0;
        if (!(mData.startNudges > 0)) mData.startNudges = 0;
        if (!(mData.nudges > 0)) mData.nudges = 0;
        if (!(mData.maximumNudges > 0)) mData.maximumNudges = 0;
        if (!(mData.startFreeSpin > 0)) mData.startFreeSpin = 0;
        if (!(mData.freeSpin > 0)) mData.freeSpin = 0;
        if (!(mData.maximumFreeSpin > 0)) mData.maximumFreeSpin = 0;

        $.ajax({
            type: "POST",
            url: "/WebService/WSGame.asmx/GameConfigUpdate",
            data: JSON.encode(mData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (typeof (data) != "undefined" && data != null) {
                    Loading.closeProcess();
                    if (data.d == -1) {
                        jAlert("Bạn không có quyền thực hiện chức năng này!");
                    }
                    if (data.d > 0) {
                        jAlert("Cập nhật thành công!");
                        Pages_Game_ConfigGame.documentReady();
                    } else {
                        jAlert("Có lỗi xảy ra trong quá trình xử lý!");
                    }
                }
            }
        });
    });
};